<?php
ini_set ( 'session.use_cookies', 1 );
ini_set ( 'session.use_only_cookies', 1 );
session_start ();
?>

<?php

class TaiKhoanController extends Zend_Controller_Action {
	//	private $service=Application_Model_service_TaiKhoanService::getInstance();
	public function indexAction() {
		// action body
	}
	//Danh sach cac Action
	//successAction
	//checkValidusername
	//updatesuccessAction
	//removeAction
	//activeAction
	//checkAction
	//checkloginAction
	//checkroleAction
	//searchAction
	//lietkeAction
	//getquyenAction
	//ganquyenAction
	//searchbyusernameAction
	//searchbynameAction
	//searchnangcaoAction
	//newAction
	//loginAction
	//updateAction
	//manageAction
	

	public function successAction() {
		
		$username = $this->_request->getPost ( "username" );
		$password = $this->_request->getPost ( "password" );
		$hoten = $this->_request->getPost ( "hoten" );
		$cmnd = $this->_request->getPost ( "cmnd" );
		$diachi = $this->_request->getPost ( "diachi" );
		$sodienthoai = $this->_request->getPost ( "dienthoai" );
		$gioitinh = $this->_request->getPost ( "gioitinh" );
		$checkmail = $this->_request->getPost ( "checkmail" );
		$email = $this->_request->getPost ( "email" );
		
		$service = Application_Model_service_TaiKhoanService::getInstance ();
		$result = $service->taoTaiKhoan ( $username, $password, $hoten, $gioitinh, $cmnd, $diachi, $sodienthoai, $email, $checkmail );
		
		if ($result > 0) {
			////LOG
			$logservice = Application_Model_service_LogSystemService::getInstance ();
			$logservice->log ( "he thong vua tao tai khoan moi cho " . $username );
			$mailservice = Application_Model_service_MailService::getInstance ();
			$hashpassword = md5 ( $password );
			$message = "http://localhost/mobileShop/public/tai-khoan/active?i=" . $result . "&&p=" . $hashpassword;
			$mailservice->autosend ( $email, $message );
		
		}
		
		$this->view->result = $result;
		$this->render ( "success" );
		//				$this->_helper->viewRenderer->setNoRender ();
	}
	public function checkValidusername() {
		echo "valid username";
		$this->_helper->viewRenderer->setNoRender ();
	}
	
	public function updatesuccessAction() {
		
		$id = $_SESSION ['id'];
		
		$password = $this->_request->getPost ( "password" );
		$hoten = $this->_request->getPost ( "hoten" );
		$cmnd = $this->_request->getPost ( "cmnd" );
		$diachi = $this->_request->getPost ( "diachi" );
		$sodienthoai = $this->_request->getPost ( "dienthoai" );
		$checkmail = $this->_request->getPost ( "checkmail" );
		$email = $this->_request->getPost ( "email" );
		$arrayinformation = array ();
		$arrayinformation ["maTaiKhoan"] = $id;
		$arrayinformation ["password"] = $password;
		$arrayinformation ["hoten"] = $hoten;
		$arrayinformation ["email"] = $email;
		$arrayinformation ["cmnd"] = $cmnd;
		$arrayinformation ["diachi"] = $diachi;
		$arrayinformation ["sodienthoai"] = $sodienthoai;
		$result = - 1;
		//		echo $sodienthoai;
		$service = Application_Model_service_TaiKhoanService::getInstance ();
		$result = $service->suathongtinTaiKhoan ( $arrayinformation );
		$this->view->hoten = $hoten;
		$this->view->result = $result;
		
		$this->_helper->layout ()->disableLayout ();
		$this->render ( 'updatesuccess' );
	}
	public function removeAction() {
		$id = $_GET ['id'];
		$service = Application_Model_service_TaiKhoanService::getInstance ();
		$result = $service->xoaTaiKhoan ( $id );
		$this->view->result = $result;
		$this->render ( "result" );
	
	}
	public function activeAction() {
		$id = $_GET ['i'];
		$hashpass = $_GET ['p'];
		$service = Application_Model_service_TaiKhoanService::getInstance ();
		$taikhoan = $service->timkiemTaiKhoan ( $id );
		if ($taikhoan instanceof Application_Model_bean_TaiKhoan) {
			$password = $taikhoan->getPassword ();
			$hassp2 = md5 ( $password );
			if ($hassp2 == $hashpass)
				$service->kichhoaTtaiKhoan ( $id );
		}
		
		$this->_helper->viewRenderer->setNoRender ();
	}
	public function checkAction() {
		
		$maquyen = $_GET ['maquyen'];
		$service = Application_Model_service_TaiKhoanService::getInstance ();
		$result = $service->checkQuyen ( $maquyen );
		//		$this->view->result = $result;
		//		$this->render ( "result" );
		echo $result;
		$this->_helper->viewRenderer->setNoRender ();
	}
	public function checkloginAction() {
			$username = $this->_request->getPost ( "username" );
			$password = $this->_request->getPost ( "password" );
			$service = Application_Model_service_TaiKhoanService::getInstance ();
			
			$result = $service->login ( $username, $password );
			
			if ($result > 0) {
				$this->view->user = $username;
				$_SESSION ["id"] = $result;
				//LOG
				

				$logservice = Application_Model_service_LogSystemService::getInstance ();
				$logservice->log ( $username . " da login" );
				$this->_helper->redirector('','index');
			}
			else {
				//	echo "<script language='JavaScript'>alert('Dang nhap That Bai!');</script>";
				//	$this->render("login");
				$this->view->thongbao = "sai username hoặc password";
				$this->render ( "login" );
			}
						
	}
	public function checkroleAction() {
		$maquyen = $_GET ["maquyen"];
		$service = Application_Model_service_TaiKhoanService::getInstance ();
		$result = $service->checkQuyen ( $maquyen );
		echo $result;
		$this->_helper->viewRenderer->setNoRender ();
	}
	public function searchAction() {
		$id = $_GET ['id'];
		$service = Application_Model_service_TaiKhoanService::getInstance ();
		$result = $service->timkiemTaiKhoan ( $id );
		$this->view->result = $result;
		
		$this->render ( "result" );
		//		$this->_helper->viewRenderer->setNoRender ();
	}
	public function lietkeAction() {
		$service = Application_Model_service_TaiKhoanService::getInstance ();
		$active = 0;
		$logservice = Application_Model_service_LogSystemService::getInstance ();
		$logservice->log ( "da liet ke xong" );
		$result = $service->lietketatcaTaikhoan ( $active );
		echo count ( $result ) . "DSFDFF";
		echo "SDFDF";
		$this->_helper->viewRenderer->setNoRender ();
	}
	public function getquyenAction() {
		$id = $_SESSION ['mataikhoan'];
		$service = Application_Model_service_TaiKhoanService::getInstance ();
		$result = $service->LietKeQuyen ( $id );
		$this->view->result = $result;
		$this->view->id=$id;
		$this->render ( "getquyen" );
		
		//		$this->_helper->viewRenderer->setNoRender ();
	}
	public function ganquyenAction() {
		$id = $_SESSION ['mataikhoan'];
		echo $id;
		$service = Application_Model_service_TaiKhoanService::getInstance ();
		$k1 = $_GET ['k1'];
		$a2 = $_GET ['a2'];
		$g3 = $_GET ['g3'];
		$k2 = $_GET ['k2'];
		$a3 = $_GET ['a3'];
		$g4 = $_GET ['g4'];
		$k3 = $_GET ['k3'];
		$a4 = $_GET ['a4'];
		$g5 = $_GET ['g5'];
		$k4 = $_GET ['k4'];
		$a5 = $_GET ['a5'];
		$g6 = $_GET ['g6'];
		$k5 = $_GET ['k5'];
		$a6 = $_GET ['a6'];
		$g7 = $_GET ['g7'];
		$a7 = $_GET ['a7'];
		$g8 = $_GET ['g8'];
		$u1 = $_GET ['u1'];
		$g1 = $_GET ['g1'];
		$g9 = $_GET ['g9'];
		$a1 = $_GET ['a1'];
		$g2 = $_GET ['g2'];
		$g10 = $_GET ['g10'];
		$arraytest = array ();
		$array = array ($k1, $k2, $k3, $k4, $k5, $u1, $a1, $a2, $a3, $a4, $a5, $a6, $a7, $g1, $g2, $g3, $g4, $g5, $g6, $g7, $g8, $g9, $g10 );
		for($i = 0; $i < count ( $array ); $i ++) {
			if ($array [$i] != "" && $array [$i] != null) {
				$index = $array [$i];
				$arraytest [$index] = 1;
			}
			;
		}
		;
		//		$testarray=array("k1"=>$k1,"k2"=>$k2,"k3"=>$k3,"k4"=>$k4,"k5"=>$k5,"u1"=>$u1,
		//							"a1"=>$a1,"a2"=>$a2,"a3"=>$a3,"a4"=>$a4,
		//								"a5"=>$a5,"a6"=>$a6,"a7"=>$a7,
		//									"g1"=>$g1,"g2"=>$g2,"g3"=>$g3,"g4"=>$g4,
		//										"g5"=>$g5,"g6"=>$g6,"g7"=>$g7,"g8"=>$g8,"g9"=>$g9,"g10"=>$g10);
		$result = $service->ganQuyen ( $id, $arraytest );
		$result = $service->LietKeQuyen ( $id );
		$this->view->result = $result;
		$this->render ( "getquyen" );
		//		$this->_helper->viewRenderer->setNoRender ();
	}
	
	//Don thuan chi la Control ->View
	public function searchbyusernameAction() {
		$service = Application_Model_service_TaiKhoanService::getInstance ();
		$username = $_GET ['username'];
		$result = $service->timkiemtheousername ( $username );
		echo $result->getUsername ();
		$this->_helper->viewRenderer->setNoRender ();
	}
	public function searchbynameAction() {
		$service = Application_Model_service_TaiKhoanService::getInstance ();
		
		$name = $_GET ['ten'];
		$result = $service->timkiemtheoten ( $name );
		echo count ( $result );
		$this->view->result = $result;
		$this->_helper->viewRenderer->setNoRender ();
		$this->render ( 'searchnangcao' );
	}
	public function searchnangcaoAction() {
		$service = Application_Model_service_TaiKhoanService::getInstance ();
		$hoten = $_GET ['ten'];
		$diachi = $_GET ['diachi'];
		$email = $_GET ['email'];
		//		echo $hoten;
		

		$thongtin = array ("hoVaTen" => $hoten, "diaChi" => $diachi, "email" => $email );
		//		$test=array("hoVaTen"=>"TeiTei",
		//							"diaChi"=>"",
		//								"email"=>""
		//										);
		

		$result = $service->timkiemnangcao ( $thongtin );
		$this->view->result = $result;
		$this->render ( "searchnangcao" );
		//		echo count($result);
	//		$this->_helper->viewRenderer->setNoRender ();
	

	}
	
	public function newAction() {
	
	}
	
	public function loginAction() {
	
	}
	public function updateAction() {
		$id = $_SESSION ['id'];
		$service = Application_Model_service_TaiKhoanService::getInstance ();
		$taikhoan = $service->timkiemTaiKhoan ( $id );
		$this->view->taikhoan = $taikhoan;
		
		$this->render ( 'update' );
	
	}
	public function manageAction() {
		$service = Application_Model_service_TaiKhoanService::getInstance ();
		$unuse = $service->lietketatcaTaikhoan ( 0 );
		$use = $service->lietketatcaTaikhoan ( 1 );
		
		$this->view->unuse = $unuse;
		$this->view->uses = $use;
		$this->render ( 'manage' );
	
	}
	public function xemchitietAction() {
		//		$array=$_SESSION['list'];
		$id = $_GET ['sub'];
		$service = Application_Model_service_TaiKhoanService::getInstance ();
		$taikhoan = $service->timkiemTaiKhoan ( $id );
		//		echo $taikhoan->getMaTaiKhoan();
		

		//		$taikhoan=NULL;
		//		foreach($array as $a){
		//			$taikhoan=$a;
		//			if($taikhoan->getMaTaiKhoan()==$id){
		//				echo 1;
		//				break;
		//			}
		//		}
		$this->view->taikhoan = $taikhoan;
		$this->render ( "xemchitiet" );
		//				$this->_helper->viewRenderer->setNoRender ();
	}
	public function chitietkhachhangAction() {
		$id = $_SESSION ['id'];
		$service = Application_Model_service_TaiKhoanService::getInstance ();
		$service2 = Application_Model_service_PhieuDatHangService::getInstance ();
		$taikhoan = $service->timkiemTaiKhoan ( $id );
		$dsphieudh = $service2->getAllPhieuHang ( $id );
		$this->view->taikhoan = $taikhoan;
		$this->view->dsphieudh = $dsphieudh;
		$this->render ( 'chitietkhachhang' );
		
	//			$this->_helper->viewRenderer->setNoRender ();
	

	}
	public function readlogAction(){
		$service = Application_Model_service_LogSystemService::getInstance ();
		 
		$service->readLog();
		$this->_helper->viewRenderer->setNoRender ();
	}
	public function taoadminAction(){
		
	}
	public function successadminAction(){
	$username = $this->_request->getPost ( "username" );
		$password = $this->_request->getPost ( "password" );
		$hoten = $this->_request->getPost ( "hoten" );
		$cmnd = $this->_request->getPost ( "cmnd" );
		$diachi = $this->_request->getPost ( "diachi" );
		$sodienthoai = $this->_request->getPost ( "dienthoai" );
		$gioitinh = $this->_request->getPost ( "gioitinh" );
		$checkmail = $this->_request->getPost ( "checkmail" );
		$email = $this->_request->getPost ( "email" );
		
		$service = Application_Model_service_TaiKhoanService::getInstance ();
		$result = $service->taoTaiKhoan ( $username, $password, $hoten, $gioitinh, $cmnd, $diachi, $sodienthoai, $email, $checkmail );
		
		if ($result > 0) {
			////LOG
			$logservice = Application_Model_service_LogSystemService::getInstance ();
			$logservice->log ( "he thong vua tao tai khoan moi cho " . $username );
			$mailservice = Application_Model_service_MailService::getInstance ();
			$hashpassword = md5 ( $password );
			$message = "http://localhost/mobileShop/public/tai-khoan/active?i=" . $result . "&&p=" . $hashpassword;
			$mailservice->autosend ( $email, $message );
		
		}
		
		$this->view->result = $result;
		$this->render ( "success" );
		//				$this->_helper->viewRenderer->setNoRender ();
	}
		public function logoutAction()
		{
			$_SESSION['id']=null;
			$this->_helper->redirector('index','index');
		}
//		public function isadminAction(){
//			$id= $_SESSION['id'];
//			$service = Application_Model_service_TaiKhoanService::getInstance ();
//			$service->isAdmin();
//			$this->_helper->viewRenderer->setNoRender ();
//		}
}

